<template>
  <el-dialog :visible.sync="openModal" width="60%" :close-on-click-modal="false">
    <el-form :model="formData" label-position="right" label-width="150">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="附加费方案名称">
            <el-input v-model="formData.title" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生效时间" required>
            <el-date-picker
              v-model="formData.effectiveFrom"
              type="datetime"
              placeholder="开始日期"
              format="yyyy-MM-dd HH:mm:ss"
              style="width:100%;"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="模板">
            <el-select v-model="formData.surchargeTypes" multiple filterable clearable style="width: 100%;" @change="surchargeTypesChanged">
              <el-option v-for="item in surchargeTemplateOptions" :key="item.value" :value="item.value" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div>
      <div style="margin-top: 20px;">
        <span> 不叠加费用组：</span>
        <el-select v-model="formData.chargeIds" multiple style="width: 100%" placeholder="请选择" >
          <el-option v-for="item2 in chargeSubjectOptions" :key="item2.id" :label="item2.name" :value="item2.id" />
        </el-select>
      </div>
    </div>
    <div v-if="formData.items.length > 0">
      <el-card v-for="item in formData.items" :key="item.surchargeType" style="margin-top: 20px;">
        <div slot="header">
          <el-row>
            <el-col :span="8">
              <el-select v-model="item.chargeType" placeholder="请选择费用科目">
                <el-option v-for="item2 in chargeSubjectOptions" :key="item2.id" :label="item2.name" :value="item2.id" />
              </el-select>
            </el-col>
          </el-row>
        </div>
        <form-create :rule="item.dataJson" :option="option" :value.sync="item.vars" />
      </el-card>
    </div>
    <div slot="footer">
      <el-button @click="openModal=false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { copyProperty } from '@/utils/tools'
export default {
  data() {
    return {
      openModal: false,
      formDataDefault: {
        logId: '',
        title: '',
        surchargeTypes: [],
        effectiveFrom: '',
        chargeIds: [],
        items: []
      },
      formData: {
        logId: '',
        title: '',
        surchargeTypes: [],
        effectiveFrom: '',
        chargeIds: [],
        items: []
      },
      surchargeTemplateOptions: [],
      chargeSubjectOptions: [],
      option: {
        submitBtn: false
      },
      ExpenseTemplateList: [] // 判断是加还是减
    }
  },
  created() {
    this.loadSurchargeTemplateOptions()
    this.loadChargeSubjectOptions()
  },
  methods: {
    submit() {
      request({
        url: `/channel/surcharge`,
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.clean()
          this.openModal = false
          this.$emit('query')
        }
      })
    },
    show() {
      this.openModal = true
    },
    loadChargeSubjectOptions() {
      request({
        url: 'charge-subject/option',
        method: 'get'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.chargeSubjectOptions = res.data
          }
        }
      })
    },
    effectiveFromChanged(v) {
      this.formData.effectiveFrom = v
    },
    loadSurchargeTemplateOptions() {
      request({
        url: `/channel/surcharge/type/option`,
        method: 'get'
      }).then(res => {
        if (res.success) {
          this.surchargeTemplateOptions = res.data
        }
      })
    },
    surchargeTypesChanged(value) {
      if (this.ExpenseTemplateList.length === value.length) {
        return
      }
      if (this.ExpenseTemplateList.length < value.length) {
        // 加东西了
        const val = value[value.length - 1]
        this.getDynamicForm(val)
      } else {
        // 减东西了
        if (this.ExpenseTemplateList.length - 1 !== value.length) {
          this.formData.items = []
          this.ExpenseTemplateList = []
          return
        }
        const val = this.findVal(this.ExpenseTemplateList, value)
        const index = this.formData.items.findIndex((item) => item.surchargeType === val)
        this.formData.items.splice(index, 1)
      }
      this.ExpenseTemplateList = value
    },
    findVal(oldArr, newArr) {
      let i = 0
      while (i < oldArr.length) {
        if (oldArr[i] !== newArr[i]) {
          return oldArr[i]
        }
        i++
      }
    },
    getDynamicForm(surchargeType) {
      request({
        url: `/channel/surcharge/dynamic-form?surchargeType=` + surchargeType,
        method: 'get'
      }).then(res => {
        if (res.success) {
          this.formData.items.push({
            chargeType: undefined,
            surchargeType: surchargeType,
            vars: {},
            dataJson: JSON.parse(res.data)
          })
        }
      })
    },
    clean() {
      copyProperty(this.formData, this.formDataDefault)
      this.ExpenseTemplateList = []
    },
    edit(logId) {
      this.formData.logId = logId
      request({
        url: `/channel/surcharge/detail?logId=${logId}`,
        method: 'get'
      }).then(res => {
        if (res.success) {
          res.data.items.forEach((item) => {
            item.dataJson = JSON.parse(item.form)
            delete item.form
          })
          copyProperty(this.formData, res.data)
          this.ExpenseTemplateList = res.data.surchargeTypes
          this.openModal = true
        }
      })
    }
  }
}
</script>
